package com.aliyun.oss.UserService.impl;

import com.aliyun.oss.UserMapper.DishMapper;
import com.aliyun.oss.UserMapper.SetmealMapper;
import com.aliyun.oss.UserMapper.ShoppingCartMapper;
import com.aliyun.oss.UserService.ShoppingCartService;
import com.aliyun.oss.pojo.DTO.ShoppingCartDTO;
import com.aliyun.oss.pojo.Entity.Food;
import com.aliyun.oss.pojo.Entity.SetMeal;
import com.aliyun.oss.pojo.Entity.ShoppingCart;
import com.aliyun.oss.utils.GetNowTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Override
    public void add(Integer id, ShoppingCartDTO shoppingCartDTO) {
//        判断当前加入到购物车中的商品是否已经存在了
        QueryWrapper<ShoppingCart> eq = new QueryWrapper<ShoppingCart>().eq("user_id", id)
                .eq(shoppingCartDTO.getDishId() != null, "dish_id", shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getDishFlavor() != null, "dish_flavor", shoppingCartDTO.getDishFlavor())
                .eq(shoppingCartDTO.getSetmealId() != null, "setmeal_id", shoppingCartDTO.getSetmealId());
        ShoppingCart one = this.getOne(eq);
//        如果已经存在了，只需要将数量加一
        if (one != null) {
            one.setNumber(one.getNumber() + 1);
            updateById(one);
        }
//        如果不存在，需要新增
        else{
//            判断本次添加到购物车的是菜品还是套餐
            Integer dishId= shoppingCartDTO.getDishId();
            LocalDateTime nowTime = GetNowTime.getNowTime();
            if(dishId!=null){
//                本次添加的是菜品
                Food food = dishMapper.selectById(dishId);
                String name = food.getName();
                String image = food.getImage();
                BigDecimal price = food.getPrice();
                ShoppingCart shoppingCart = new ShoppingCart(name,image,id,dishId,shoppingCartDTO.getDishFlavor(),1,price,nowTime);
                save(shoppingCart);

            }else{
//                本次添加的是套餐
                SetMeal setMeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                String name = setMeal.getName();
                String image = setMeal.getImage();
                BigDecimal price = setMeal.getPrice();
                ShoppingCart shoppingCart = new ShoppingCart(name, image, id, shoppingCartDTO.getSetmealId(), 1, price, nowTime);
                save(shoppingCart);
            }
        }
    }

    @Override
    public List<ShoppingCart> list(Integer id) {
        QueryWrapper<ShoppingCart> eq = new QueryWrapper<ShoppingCart>().eq("user_id", id);
        List<ShoppingCart> shoppingCarts = this.list(eq);
        return shoppingCarts;
    }

    @Override
    public void reduce(Integer id, Integer shoppingCartId, Integer number) {
        QueryWrapper<ShoppingCart> eq = new QueryWrapper<ShoppingCart>().eq("id", shoppingCartId).eq("user_id", id);
        ShoppingCart one = this.getOne(eq);
        one.setNumber(number);
        updateById(one);
    }
}
